﻿Public Class fCanales
    Dim daCanales As lbGlobalcom.dsFacturacionTableAdapters.FAC_CANALESTableAdapter
    Dim daClave As lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter

    Private Sub fCanales_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Me.DsFacturacion.FAC_CANALES.GetChanges IsNot Nothing Then
                FAC_CANALESBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    FAC_CANALESBindingNavigatorSaveItem.PerformClick()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fCanales_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daCanales = New lbGlobalcom.dsFacturacionTableAdapters.FAC_CANALESTableAdapter
            daClave = New lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
            Dim daListas As New lbGlobalcom.dsProductosTableAdapters.PRO_ENCALISTAPRECIOSTableAdapter
            daListas.FillByTipoLista(DsProductos.PRO_ENCALISTAPRECIOS, 1)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub FAC_CANALESBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FAC_CANALESBindingNavigatorSaveItem.Click
        Try
            Validate()
            daCanales.Update(DsFacturacion.FAC_CANALES)
            MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If MessageBox.Show("Desea borrar el registro", "Elimina registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            FAC_CANALESBindingSource.Remove(FAC_CANALESBindingSource.Current)
            FAC_CANALESBindingNavigatorSaveItem.PerformClick()
        End If
    End Sub

    Private Sub FAC_CANALESDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles FAC_CANALESDataGridView.CellBeginEdit
        Try
            If e.ColumnIndex = 1 AndAlso FAC_CANALESDataGridView.Rows(e.RowIndex).Cells(0).Value Is DBNull.Value Then
                Dim dt As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASDataTable
                dt = daClave.GetDataByIdClavePrimaria("FAC_CANALES")
                If dt.Count Then
                    Dim dr As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASRow
                    dr = dt.Rows(0)
                    Dim Cons As lbGlobalcom.ConsecTablas = New lbGlobalcom.ConsecTablas(dr("TABLA"), dr("CAMPO"), dr("LONGITUD"))
                    FAC_CANALESDataGridView.Rows(e.RowIndex).Cells(0).Value = Cons.NumeroConsec
                    Cons = Nothing
                    FAC_CANALESBindingSource.EndEdit()
                    daCanales.Update(DsFacturacion.FAC_CANALES)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub FAC_CANALESDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles FAC_CANALESDataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub FAC_CANALESDataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles FAC_CANALESDataGridView.UserDeletedRow
        Try
            daCanales.Update(DsFacturacion.FAC_CANALES)
            MessageBox.Show("Registro borrado con exito", "Elimina regsitro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub FAC_CANALESDataGridView_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles FAC_CANALESDataGridView.UserDeletingRow
        If MessageBox.Show("Desea borrar el registro", "Elimina registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub

    Private Sub btnCargarCanales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarCanales.Click
        Try
            If Me.DsFacturacion.FAC_CANALES.GetChanges IsNot Nothing Then
                FAC_CANALESBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    FAC_CANALESBindingNavigatorSaveItem.PerformClick()
                End If
            End If
            daCanales.Fill(DsFacturacion.FAC_CANALES)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
End Class